//
//  ZYPurchaseTransactionModel.h
//  ZYPurchase
//
//  Created by wu on 2020/8/6.
//  Copyright © 2020 wu. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ZYPurchaseOrderModel.h"

@class FMResultSet;

typedef NS_ENUM(NSInteger, ZYPurchaseTransactionStatus) {
    ZYPurchaseTransactionStatus_Idle = 0,        // 空闲状态，交易创建后还未发起的默认状态
    ZYPurchaseTransactionStatus_TryPurchage,      // 已经发起购买，等待Apple回调正在支付中状态
    ZYPurchaseTransactionStatus_Purchasing,      // 购买中（包含交易队列中及已发起待添加至交易队列中的交易）
    ZYPurchaseTransactionStatus_Verifing,        // IAP结束，提交后台校验中（包含交易队列中及已发起待添加至交易队列中的交易）
    ZYPurchaseTransactionStatus_Cancel,          // 交易已取消
    ZYPurchaseTransactionStatus_Fail,            // 交易失败
    ZYPurchaseTransactionStatus_ReceiptInvalid,     // 交易结束，收据校验不通过
    ZYPurchaseTransactionStatus_ReceiptWaitVerify,     // 交易结束，收据尚未校验（非网络原因）
    ZYPurchaseTransactionStatus_ReceiptWaitVerify_NetworkLost,     // 交易结束，收据尚未校验（网络原因）
    ZYPurchaseTransactionStatus_Success,         // 交易已经完成，且收据校验成功（包含内购商品中已经购买过的情况）
    ZYPurchaseTransactionStatus_Restored,        // 续费完成  10
    ZYPurchaseTransactionStatus_RePurchase,        // 重复购买
    ZYPurchaseTransactionStatus_BindByOthers,        // 被其他用户绑定
};

typedef NS_ENUM(NSInteger, ZYPurchasePaymentMethod) {
    ZYPurchasePaymentMethod_IAP,                // 内购
    ZYPurchasePaymentMethod_ApplePay,           // ApplePay
    ZYPurchasePaymentMethod_UnionPay,           // 银联卡支付
    ZYPurchasePaymentMethod_AliPay,             // 支付宝
    ZYPurchasePaymentMethod_WeChatPay,          // 微信支付
};

NS_ASSUME_NONNULL_BEGIN

@interface ZYPurchaseTransactionModel : NSObject

// 数据库中主键id
@property (nonatomic, assign, readonly) NSInteger sql_id;

/// 交易支付方式
@property (nonatomic, assign) ZYPurchasePaymentMethod paymentMethod;

/// 交易状态
@property (nonatomic, assign) ZYPurchaseTransactionStatus transactionStatus;

/// 交易订单信息
@property (nonatomic, strong, readonly) ZYPurchaseOrderModel *order;
@property (nonatomic, copy, readonly) NSString *orderId;

// Apple 返回的交易id
@property (nonatomic, copy) NSString *transactionId;
@property (nonatomic, copy) NSString *transactionId_origin;

/// 最新收据（IAP支付后系统返回，用于提交后台（后台调用App Store Connect 接口校验收据））
@property (nonatomic, copy) NSString *receipt_data_filename;

// 失败描述
@property (nonatomic, copy) NSString *error;

/// 校验结果
@property (nonatomic, strong) NSDictionary *receiptResult;

@property (nonatomic, copy, readonly) NSString *uuid;

@property (nonatomic, assign) double createTime;
@property (nonatomic, assign) double updateTime;

+ (instancetype)formatIAPTransaction:(nonnull ZYPurchaseOrderModel *)order;




// 实例化
+ (instancetype)modelFromResultSet:(FMResultSet *)resultSet;

/// 开始处理订单
- (BOOL)startPayment;

- (BOOL)isValuable;

// 状态值转状态描述
+ (NSString *)transactionStatusDescription:(ZYPurchaseTransactionStatus)status;

@end

@interface ZYPurchaseTransactionModel (ApplicationUsername)

// 组装支付的 ApplicationUsername
- (NSString *)formatApplicationUsername;
// ApplicationUsername 中获取用户id
+ (NSString *)fetchUserIdFromApplicationUsername:(nonnull NSString *)applicationUsername;
// ApplicationUsername 中获取订单id
+ (NSString *)fetchOrderIdFromApplicationUsername:(nonnull NSString *)applicationUsername;

@end

@interface ZYPurchaseTransactionModel (Receipt)

// 获取收据存储的沙盒路径
- (nullable NSString *)receiptDataFilePath;
+ (nullable NSString *)receiptDataFilePathForFileName:(nonnull NSString *)fileName;
// 生成收据名称
+ (NSString *)generateReceiptDataFileName;
// 本地存储的收据
- (nullable NSString *)receiptDataLocal;
// 根据收据文件名称获取收据字符串
+ (nullable NSString *)receiptStringWithName:(NSString *)fileName;
// 存储收据到本地
+ (BOOL)saveReceiptData:(nonnull NSString *)receiptInfo
                     at:(nonnull NSString *)filePath;

@end

NS_ASSUME_NONNULL_END
